package com.rocketmq.demo.scheduled;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @ProjectName: rocketmq-demo
 * @Package: com.rocketmq.demo.scheduled
 * @ClassName: ScheduledMessageProducer
 * @Author: mac
 * @Description:
 * @Date: 2021/7/1 10:19 下午
 * @Version: 1.0
 */
public class ScheduledMessageProducer {

    public static void main(String[] args) {
        //实例化一个生产者来生产延时消息
        DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
        producer.setNamesrvAddr("192.168.31.67:9876;192.168.31.126:9876;192.168.31.209:9876");

        try {
            //启动生产者
            producer.start();
            //要发消息的总数
            int totalMessagesToSend = 100;
            for (int i = 0; i < totalMessagesToSend; i++) {
                Message message = new Message("TestTopic",
                        ("Hello scheduled message " + i).getBytes());
                // 设置延时等级3,这个消息将在10s之后发送(现在只支持固定的几个时间,详看delayTimeLevel)
                message.setDelayTimeLevel(3);
                // 发送消息
                producer.send(message);

            }



        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        producer.shutdown();
        //关闭生产者

    }
}
